package com.seele.tigerwallet.ui.activtiy;

import android.graphics.Color;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.api.APIException;
import com.seele.tigerwallet.api.APIModule;
import com.seele.tigerwallet.api.APIObjectCallBack;
import com.seele.tigerwallet.api.APIStringCallBack;
import com.seele.tigerwallet.api.BaseResult;
import com.seele.tigerwallet.model.ContactsModel;
import com.seele.tigerwallet.model.UserInfo;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import com.seele.tigerwallet.ui.base.ToolbarBaseActivity;
import com.seele.tigerwallet.ui.manager.DaoManager;
import com.seele.tigerwallet.ui.utils.BaseUtil;
import com.seele.tigerwallet.ui.utils.GsonUtil;
import com.seele.tigerwallet.ui.utils.SPUtils;
import com.seele.tigerwallet.ui.utils.SystemUtils;
import com.seele.tigerwallet.ui.views.LineEditText;
import com.seele.tigerwallet.wallet.WalletManager;

import org.greenrobot.eventbus.EventBus;
import org.spongycastle.crypto.agreement.srp.SRP6Client;

import java.sql.SQLException;

import jnr.x86asm.RID;
import retrofit2.Call;

public class CreateContactsActivity extends ToolbarBaseActivity {


    private String surname;
    private String toAddress;
    private String remarks;
    private TextInputLayout mNameTextInputLayout;
    private TextInputLayout mRemarksTextInputLayout;
    private TextInputLayout mAddressTextInputLayout;
    private AppCompatEditText mNameEdt;
    private AppCompatEditText mAddress;
    private ContactsModel mContactsModel;


    @Override
    public int getContainerViewId() {

        return R.layout.activity_create_contacts;

    }

    @Override
    protected void initView() {
        setTitle(R.string.create_contacts);
        mNameTextInputLayout = $f(R.id.name_account);
        mRemarksTextInputLayout = $f(R.id.remarks);
        mAddressTextInputLayout = $f(R.id.address);
        mNameEdt = $f(R.id.name_edt);
        mAddress = $f(R.id.address_edt);
        setRightText(R.string.save);
        setRightTextColor(R.color.color_63D0AB);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            mContactsModel = (ContactsModel) bundle.getSerializable(CreateContactsActivity.class.getSimpleName());
            if (null != mContactsModel) {
                initContacts();
            }
        }

    }

    private void initContacts() {
        mNameTextInputLayout.getEditText().setText(mContactsModel.getFirstname());
        mRemarksTextInputLayout.getEditText().setText(mContactsModel.getNote());
        mAddressTextInputLayout.getEditText().setText(mContactsModel.getToaddr());
    }


    @Override
    protected void onRightText() {
        surname = mNameTextInputLayout.getEditText().getText().toString();
        remarks = mRemarksTextInputLayout.getEditText().getText().toString();
        toAddress = mAddressTextInputLayout.getEditText().getText().toString();

        if (TextUtils.isEmpty(surname)) {
//            showError(mNameTextInputLayout,getResources().getString(R.string.surname_error));
            Toast.makeText(this, getResources().getString(R.string.surname_error), Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(toAddress)) {
            Toast.makeText(this, getResources().getString(R.string.address_error), Toast.LENGTH_SHORT).show();
            return;
        }
        if (!SPUtils.isAddress(toAddress)) {
            Toast.makeText(this, getResources().getString(R.string.address_correct_error), Toast.LENGTH_SHORT).show();
            return;
        }

        String address = UserInfo.get().address;
        if (TextUtils.isEmpty(address)) {
            return;
        }


        if (null == mContactsModel) {
            surname = BaseUtil.makeToBase64(surname);
            remarks = BaseUtil.makeToBase64(remarks);
            addlinkman(address);
        } else {
            surname = BaseUtil.makeToBase64(surname);
            remarks = BaseUtil.makeToBase64(remarks);
            updatelinKman(address, mContactsModel.id+"");
        }


    }

    private void updatelinKman(final String address, final String id) {
        mRightContainer.setClickable(false);
        APIModule
                .getApi()
                .updatelinkman(UserInfo.get().token, id, address, surname, surname, toAddress, "", "", remarks)
                .enqueue(new APIObjectCallBack<BaseResult>() {
                    @Override
                    protected void onResponseError(APIException e) {
                        mRightContainer.setClickable(true);

                    }

                    @Override
                    protected void onFailureError(Call<String> call, Throwable t) {
                        mRightContainer.setClickable(true);

                    }

                    @Override
                    protected void onResponse(BaseResult response) {
                        mRightContainer.setClickable(true);
                        if (null != response) {
                            try {
                                UserInfo.get().token = response.token;
                                SPUtils.saveUserInfo(UserInfo.get());
                                finish();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    }
                });


    }

    /**
     * 添加联系人成功
     *
     * @param address
     * @param id
     * @throws SQLException
     */
    private void createOrUpdate(String address, String id) throws SQLException {
        mContactsModel.address = address;
        mContactsModel.firstname = surname;
        mContactsModel.lastname = surname;
        mContactsModel.note = remarks;
        mContactsModel.toaddr = toAddress;

        DaoManager.saveOrUpdate(mContactsModel);
    }


    private void addlinkman(String address) {
        mRightContainer.setEnabled(false);
        APIModule
                .getApi()
                .addlinkman(UserInfo.get().token, address,
                        surname, surname, toAddress, "", "", remarks)
                .enqueue(new APIObjectCallBack<BaseResult>() {
                    @Override
                    protected void onResponseError(APIException e) {
                        mRightContainer.setClickable(true);

                    }

                    @Override
                    protected void onFailureError(Call<String> call, Throwable t) {
                        mRightContainer.setClickable(true);
                    }

                    @Override
                    protected void onResponse(BaseResult response) {
                        mRightContainer.setClickable(true);
                        if (null != response) {
                            UserInfo.get().token = response.token;
                            SPUtils.saveUserInfo(UserInfo.get());
                            finish();
                        }

                    }
                });
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().post(new ContactsModel());
        super.onDestroy();
    }
}
